package com.jhy.suixianglu.daima20230115;

public class ReverseLeftWords2 {
    public static String reverseLeftWords(String s, int n) {
        // 反转区间为前n的子串
        char[] chars = s.toCharArray();
        resver(chars, 0, n - 1);
        // 反转区间为n到末尾的子串
        resver(chars, n, chars.length - 1);
        // 反转整个字符串
        resver(chars, 0, chars.length - 1);
        return new String(chars);
    }

    private static void resver(char[] chars, int i, int n) {
        while (i < n) {
            char temp = chars[i];
            chars[i] = chars[n];
            chars[n] = temp;
            i++;
            n--;
        }
    }

    public static void main(String[] args) {
        String  s = "abcdefg";
        String s1 = reverseLeftWords(s, 2);
        System.out.println(s1);
    }
}
